#ifndef OPENGLWIDGET_H
#define OPENGLWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLVertexArrayObject>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QElapsedTimer>

class OpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    explicit OpenGLWidget(QWidget *parent = nullptr);
    ~OpenGLWidget();

    // 控制接口（供外部调用）
    void setShowTriangle(bool show);
    void setShowSquare(bool show);
    void setRotationAngle(float angle);
    void setColorFactor(float factor);

protected:
    void initializeGL() override;
    void paintGL() override;
    void resizeGL(int w, int h) override;
    void keyPressEvent(QKeyEvent *event) override;
    void keyReleaseEvent(QKeyEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;

private:
    void initShaders();
    void initGeometry();

    QOpenGLShaderProgram *m_program;
    QOpenGLVertexArrayObject m_vao;
    QOpenGLBuffer m_vbo_axes;

    QMatrix4x4 m_proj;
    QMatrix4x4 m_view;
    QMatrix4x4 m_model;

    QVector3D m_cameraPos;
    QVector2D m_lastMousePos;
    float m_zoom;

    bool m_keys[256] = {false};
    bool m_showTriangle = false;
    bool m_showSquare = false;
    float m_rotationAngle = 0.0f; // 外部控制的旋转
    float m_colorFactor = 0.5f;   // 颜色插值因子

    QElapsedTimer m_timer; // 添加计时器
};
#endif // OPENGLWIDGET_H